const app = getApp();
let filter = require('../../../utils/intercept.js')
const http = require('../../../utils/http.js')

Page(filter.identityFilter({

    /**
     * 页面的初始数据
     */
    data: {
        loadModal: false,
        isShow: false,
        outTradeNo:"",
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        coupon: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        http.get('/api/index/selectCouponDetail', {stockId: options.stockId}).then(res => {
            if (res.code == 200) {
                this.setData({
                    coupon: res.result
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    buyCoupon() {
        let that = this;
        if (!app.globalData.userInfo.payOpenid) {
            //重定向到指定的页面
            wx.navigateTo({
                url: "/pages/auth/openid"
            });
        }else {
            this.setData({loadModal: true})
            http.get('/api/index/sendCoupon', {stockId: this.data.coupon.stockId}).then(res => {
                this.setData({loadModal: false})
                if (res.code == 200) {
                    //需要购买发起支付
                    if (this.data.coupon.type == 2) {
                        let outTradeNo = res.result.outTradeNo
                        wx.requestPayment({
                            timeStamp: res.result.timeStamp,
                            nonceStr: res.result.nonceStr,
                            package: res.result.packageValue,
                            signType: res.result.signType,
                            paySign: res.result.paySign,
                            success(res) {
                                if (res.errMsg = "requestPayment:ok") {
                                    that.successAfter(outTradeNo)
                                }
                            }
                        })
                    } else {
                        //不需要购买发放成功
                        this.successAfter(res.result)
                    }
                }else {
                    wx.showModal({
                        content: res.message,
                        showCancel: false,
                    })
                }
            })
        }
    },
    //
    successAfter(outTradeNo) {
        let that = this;
        if (that.data.coupon.stockType == 'merNORMAL') { //如果是商家券,过1秒去查询是否支付成功
            setTimeout(function () {
                that.setData({
                    isShow: true,
                    outTradeNo: outTradeNo
                })
            }, 800)
        } else { //不是商家券提示购买成功
            wx.showToast({
                title: '购买成功',
                icon: 'success'
            })
        }
        //减去详情页面数量
        let data = Object.assign(this.data.coupon, {distributedCoupons: this.data.coupon.distributedCoupons + 1})
        this.setData({
            loadModal: false,
            coupon: data
        })
    },

    //分享
    onShareAppMessage() {
        console.log(this.data.coupon.stockId)
        return {
            title: this.data.coupon.stockName,
            //imageUrl: '/images/share.jpg',
            path: '/pages/home/couponDetail/index?stockId=' + this.data.coupon.stockId
        }
    },

}))
